/**
 * 版本管理初始化
 */
var VpnAppVersion = {
    id: "VpnAppVersionTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
VpnAppVersion.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '序号', field: 'versionId', visible: false, align: 'center', valign: 'middle'},
            {title: '版本号', field: 'versionNo', visible: true, align: 'center', valign: 'middle'},
            // {title: '', field: 'status', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {title: '修改时间', field: 'updateTime', visible: false, align: 'center', valign: 'middle'},
            {title: '终端设备', field: 'platform', visible: true,
                align: 'center', valign: 'middle',formatter: function (value, row, index) {
                    if(value == '1'){
                        return "Android"
                    }
                    else if(value == '2'){
                        return "IOS"
                    }
                    else if(value == '3'){
                        return "Windows"
                    }
                    else if(value == '4'){
                        return "Mac"
                    }
                }},
            {title: '下载路径', field: 'downloadUrl', visible: true, align: 'center', valign: 'middle'},
            {title: '更新状态', field: 'isForce', visible: true,
                align: 'center', valign: 'middle',formatter: function (value, row, index) {
                    if(value == '0'){
                        return "不强制更新"
                    }
                    else if(value == '1'){
                        return "强制更新"
                    }
                }},
            {title: '操作', field: 'versionDesc',
                    visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index) {
                    var content = row.versionDesc;
                    if (content !== '' && content !== null){
                        return "<a onclick=\"preview('"+content.split('\n')+"')\">预览更新描述</a>";
                    }else{
                        return '未添加描述'
                    }
            }}
    ];
};

function preview(row){
    var c = row.split(",");
    const len = c.length;
    var str="";
    for (let i = 0; i < len; i++) {
        str += c[i] + "<br/>"
    }
    var content = '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'+str+'</div>';
    layer.open({
        type: 1
        ,title: false //不显示标题栏
        ,closeBtn: false
        ,area: '500px;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: content
        ,success: function(layero){

        }
    });
}

/**
 * 检查是否选中
 */
VpnAppVersion.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        VpnAppVersion.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加版本管理
 */
VpnAppVersion.openAddVpnAppVersion = function () {
    var index = layer.open({
        type: 2,
        title: '添加版本',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnAppVersion/vpnAppVersion_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看版本管理详情
 */
VpnAppVersion.openVpnAppVersionDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '更新版本信息',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/vpnAppVersion/vpnAppVersion_update/' + VpnAppVersion.seItem.versionId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除版本管理
 */
VpnAppVersion.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/vpnAppVersion/delete", function (data) {
            Feng.success("删除成功!");
            VpnAppVersion.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("vpnAppVersionId",this.seItem.versionId);
        ajax.start();
    }
};

/**
 * 查询版本管理列表
 */
VpnAppVersion.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    VpnAppVersion.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = VpnAppVersion.initColumn();
    var table = new BSTable(VpnAppVersion.id, "/vpnAppVersion/list", defaultColunms);
    table.setPaginationType("client");
    VpnAppVersion.table = table.init();
});
